//
//  RootController.m
//  MobilBlogg
//
//  Created by Tobias Rundström on 2009-10-22.
//  Copyright 2009 Purple Scout. All rights reserved.
//

#import "RootController.h"
#import "NeedishStore.h"
#import "INDefaultStylesheet.h"


@implementation RootController

- (id)init {
	self = [super init];
	
	self.title = @"iNeedish";
	self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" 
																			  style:UIBarButtonItemStyleBordered 
																			 target:nil action:nil] autorelease];
//	self.tableViewStyle = UITableViewStyleGrouped;
	self.statusBarStyle = UIStatusBarStyleBlackOpaque;
	self.navigationBarTintColor = TTSTYLEVAR(needishBlueColor);
	self.variableHeightRows = YES;
	self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;		
	
	_activity = [[TTActivityLabel alloc] initWithFrame:CGRectMake(0, 0, 320, 416)
												 style:TTActivityLabelStyleBlackBox
												  text:NSLocalizedString(@"Loading...", nil)];
	return self;
}


- (void)createModel
{
	self.dataSource = [TTSectionedDataSource dataSourceWithObjects:
					   NSLocalizedString(@"MobilBlogg", nil),
					   [TTTableTextItem itemWithText:NSLocalizedString(@"My Needs", nil) URL:@"in://tabBar/"],
					   [TTTableTextItem itemWithText:NSLocalizedString(@"My Blog",nil) URL:@"in://menu/1"],					   
					   [TTTableTextItem itemWithText:NSLocalizedString(@"Go to User", nil) URL:@"in://menu/2"],
					   [TTTableTextItem itemWithText:NSLocalizedString(@"First Page", nil) URL:@"in://menu/3"],
					   nil];
}

- (void)viewDidLoad {
	NSString *username, *password;

	username = [NeedishStore getUserName];
	password = [NeedishStore getPasswordForUsername:username];
	
	if (!username || !password) {
		TTLOG(@"We open userconf");
		[[TTNavigator navigator] openURL:@"in://userconfmodal" animated:NO];
	} else {
		TTLOG(@"We login");		
		NeedishLogin *login = [NeedishLogin loginWithUsername:username andPassword:password];
		login.delegate = self;
		
		self.navigationItem.rightBarButtonItem.enabled = NO;
		[[self view] addSubview:_activity];
	}
}

-(void)loginDidSucceed
{
	[_activity removeFromSuperview];
	self.navigationItem.rightBarButtonItem.enabled = YES;
}

-(void)loginDidFailWithError:(NSError *)err
{
	UIAlertView *alert;
	[_activity removeFromSuperview];
	self.navigationItem.rightBarButtonItem.enabled = YES;
	
	
	if ([[err domain] isEqualToString:@"ineedish"] && [err code] == 1) {
		alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Login failed", nil)
										   message:NSLocalizedString(@"Saved credentials are not valid", nil)
										  delegate:self
								 cancelButtonTitle:NSLocalizedString(@"Ok", nil)
								 otherButtonTitles:NSLocalizedString(@"Settings", nil),nil];
	} else {
		alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Login failed", nil)
										   message:[err localizedDescription]
										  delegate:self
								 cancelButtonTitle:NSLocalizedString(@"Ok", nil)
								 otherButtonTitles:nil];

	}

	[alert show];
	[alert release];
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
	if (buttonIndex == 0) {
		/*TODO: make something fancy here later */
	} else {
		[[TTNavigator navigator] openURL:@"in://userconfmodal" animated:YES];
	}
}

- (void)dealloc {
	[_activity release];
    [super dealloc];
}


@end
